home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1496 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  72 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Mem Allocation in functions
  5. Date: 14 Jan 1996 18:58:42 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4dbjp2$47c@news.iag.net>
  8. References: <00001a80+00006ba7@msn.com>
  9. NNTP-Posting-Host: pm3-orl13.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <00001a80+00006ba7@msn.com>, Gordon_B@msn.com says...
  13. >
  14. >I should know the answer, but I don't. I have a function
  15. >
  16. >#include <usual stuff>
  17. > void foo(float *stuff, int *length)
  18. >   {
  19. >    int i;
  20. >    stuff = calloc(*length, sizeof(float));
  21. >//    diddle around and put numbers in stuff[i]
  22. >    return;
  23. >   }
  24. >//
  25. > int main(void)
  26. >  {
  27. >   int count;
  28. >   float *somenums;
  29. >   int np = 20;
  30. >   void foo(float *, int *);
  31. >   foo(somenums,&np);
  32. >    for(count = 0 ; count < np ; count++)
  33. >      fprintf(stdout,"%4d %10.2f\n",count, somenums[count] ;
  34. >   free(somenums);
  35. >   return 0;
  36. >  }
  37. >My question is: in foo, memory was allocated to the pointer "somenums" and
  38. >at the return the array was filled with the correct values.  However,
  39. >in main the array is junk NANs, and in fact the pointer is pointing into DS.
  40. >On the other hand, if "somenums" is calloc'd in main, correct values are
  41. >obtained.
  42.  
  43. The problem is that you are passing a copy of the value of somenums to 
  44. foo, not a pointer to somenums.  For a function to modify the value of an 
  45. object that is out of scope to the function, the function must have a pointer
  46. to that object.  So change your function definition to accept a pointer
  47. to a pointer to a float (float **stuff), modify your allocation statement to
  48. assign the return of calloc to the object pointed to by stuff (*stuff), and
  49. call the function by passing the address of the pointer you want modified
  50. (&somenums).
  51.  
  52. BTW, if you are going to need to dereference stuff often in foo, you may find
  53. it easier to troubleshoot, if you define a local pointer, assign the alloc 
  54. return to it, use it to do any testing and modifications, then just before 
  55. exiting the function assign its value to the object pointed to by your param.
  56. This requires the use of an unnecessary local variable, but, IMHO,it makes 
  57. the code much easier to read, if you don't happen to live for indirections.
  58.  
  59. void foo( float **stuff, int *length)
  60.    {
  61.    float *ptr = calloc( *length, sizeof(float));
  62.    if( ptr == NULL)
  63.       /* handle the allocation error */
  64.    /* use ptr to manipulate the element values */
  65.    *stuff = ptr;
  66.    }
  67.  
  68. -- 
  69. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  70. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  71.  
  72.